package com.kazusa.myservlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/*
* setAttribute()方法用于设置session域对象
* getAttribute()方法用于获取session域对象
* removeAttribute()方法用于移除session域对象
*
*           请求转发，一次请求
*           request作用域有效
*           session作用域有效
*
*           重定向，两次请求
*           session作用域有效
*           request作用域无效
* */
@WebServlet("/SessionAttribute")
public class SessionAttribute extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();

        //设置session域对象
        session.setAttribute("uname","admin");
        session.setAttribute("upwd","123456");

        //移除session域对象
        session.removeAttribute("upwd");

        //request域对象
        request.setAttribute("name","zhangsan");

        //请求转发跳转到jsp界面
        //request.getRequestDispatcher("sessionAttribute.jsp").forward(request,response);

        //重定向到sessionAttribute.jsp界面
        response.sendRedirect("sessionAttribute.jsp");

    }
}
